
package net.soporte.action;

import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.soporte.dao.UsuarioDAO;
import net.soporte.form.LoginForm;
import net.soporte.form.UsuarioForm;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
//TODO Documentar esta clase. NOTA: eliminar esta tarea al terminar de documentar
/**
 * Clase que : 
 *
 * Path = Pruebas--net.soporte.struts.helloworld.action.
 *
 * @author ri00642
 *
 */
public class LoginAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) 
            throws Exception { 
		HttpSession sess = request.getSession(); 
        LoginForm loginForm = (LoginForm)form;  
        UsuarioForm usuarioForm = new UsuarioForm(loginForm.getUserName(),loginForm.getPassword(),0,0);
        UsuarioDAO usuarioDAO = new UsuarioDAO();
        String target = null;
        System.out.println("usuario:" + loginForm.getUserName());
        System.out.println("password:" + loginForm.getPassword());
        int perfilUsuario=0;
        int codEmp;
        
        
        
        if (usuarioDAO.Buscar(usuarioForm)==1){
        	perfilUsuario=usuarioForm.getPerfilUsuario();
        	codEmp = usuarioDAO.BuscarId(usuarioForm);
        	request.setAttribute("message", loginForm.getUserName());
            //sess.setAttribute("usr", loginForm.getUserName());
        	sess.setAttribute("usr", new Integer (codEmp).toString());
            sess.setAttribute("pfu", new Integer(perfilUsuario).toString()); 
            
            
            
        	target = "success";
        }else{
        	loginForm.setUserName("");
        	loginForm.setPassword("");
        	target = "failure";
        }

        
        System.out.println("accion");
        System.out.println(loginForm.getAccion());
        
        return mapping.findForward(target); 
    } 

}


